import Phaser from 'phaser-ce'
export default class extends Phaser.State {
  constructor ({key}) {
    super()
    this.initState = key
  }
  init () {
    this.stage.backgroundColor = '#0a8cfd'
  }

  preload () {
    this.loadAssets()
    this.createView()
  }

  loadAssets () {
    this.load.atlas('boom', 'assets/plane/boom.png', 'assets/plane/boom.json')
    this.load.image('background', 'assets/background.png')
    this.load.image('ground', 'assets/ground.png')
    this.load.image('title', 'assets/title.png')
    this.load.spritesheet('bird', 'assets/bird.png', 34, 24, 3)
    this.load.image('btn', 'assets/start-button.png')
    this.load.spritesheet('pipe', 'assets/pipes.png', 54, 320, 2)
    this.load.bitmapFont('flappy_font', 'assets/fonts/flappyfont/flappyfont.png', 'assets/fonts/flappyfont/flappyfont.fnt')
    this.load.audio('fly_sound', 'assets/flap.wav')
    this.load.audio('score_sound', 'assets/score.wav')
    this.load.audio('hit_pipe_sound', 'assets/pipe-hit.wav')
    this.load.audio('hit_ground_sound', 'assets/ouch.wav')
    this.load.image('ready_text', 'assets/get-ready.png')
    this.load.image('play_tip', 'assets/instructions.png')
    this.load.image('game_over', 'assets/gameover.png')
    this.load.image('score_board', 'assets/scoreboard.png')
    this.load.audio('fly_sound', 'assets/flap.wav')
    this.load.audio('score_sound', 'assets/score.wav')
    this.load.audio('hit_pipe_sound', 'assets/pipe-hit.wav')
    this.load.audio('hit_ground_sound', 'assets/ouch.wav')
    this.load.onFileComplete.add((p) => {

    })
  }

  createView () {
    const preloadSprite = this.game.add.sprite(0, 0, 'loading')
    preloadSprite.top = (this.game.height - preloadSprite.height) / 2
    preloadSprite.left = (this.game.width - preloadSprite.width) / 2
    this.game.load.setPreloadSprite(preloadSprite)
  }
  create () {
    if (this.initState) {
      this.state.start(this.initState)
    } else {
      this.state.start('Start')
    }
  }
}
